package com.pine.dcts.service.impl;

import com.pine.dcts.common.TaskStatusType;
import com.pine.dcts.entity.JobInfo;
import com.pine.dcts.service.JobService;
import com.pine.dcts.service.TaskStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xiaoyuan
 * @create 2021/1/13 16:51
 **/
@Service
public class TaskStatusServiceImpl implements TaskStatusService {

    @Autowired
    private JobService jobService;

    @Transactional
    @Override
    public void prepare(Integer jobId) {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setId(jobId);
        jobInfo.setStatus(TaskStatusType.PREPARE.getValue());
        this.jobService.updateById(jobInfo);
    }

    @Transactional
    @Override
    public void running(Integer jobId) {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setId(jobId);
        jobInfo.setStatus(TaskStatusType.RUNNING.getValue());
        this.jobService.updateById(jobInfo);
    }

    @Transactional
    @Override
    public void error(Integer jobId) {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setId(jobId);
        jobInfo.setStatus(TaskStatusType.ERROR.getValue());
        this.jobService.updateById(jobInfo);
    }

    @Transactional
    @Override
    public void suspend(Integer jobId) {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setId(jobId);
        jobInfo.setStatus(TaskStatusType.SUSPEND.getValue());
        this.jobService.updateById(jobInfo);
    }

    @Transactional
    @Override
    public void finish(Integer jobId) {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setId(jobId);
        jobInfo.setStatus(TaskStatusType.FINISH.getValue());
        this.jobService.updateById(jobInfo);
    }
}
